package com.ych.rabbitmq.listener;

import com.ych.SDK.alibaba.alipaySDK.entity.AlipayQueryOrderInfo_Request;
import com.ych.modules.api.authorInfc.jackpot.service.JackpotService;
import com.ych.rabbitmq.exchange.RaffleTicketJackpotExchange;
import com.ych.redis.RedisLockUtil;
import com.ych.utils.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/3/22
 * Time: 16:38
 * Description:
 */
@Component
@RabbitListener(queues = RaffleTicketJackpotExchange.QUEUE_NAME)
@Slf4j
public class RaffleTicketJackpotExchangeListener {
    @Autowired
    private JackpotService jackpotService;

    /**
     * 延时处理
     *
     * @param jackpotId 奖池编号
     */
    @RabbitHandler
    public void orderDelayConsumer(String jackpotId) {
        String lockName = "Lock_Jackpot_" + jackpotId;
        String lockId = UUID.randomUUID();
        if (RedisLockUtil.tryLock(lockName, lockId, 300)) {
            try {
                jackpotService.runLottery(jackpotId);
            } catch (Exception e) {
                log.error("延时奖池发放错误信息{},奖池号:{}", e.getMessage(), jackpotId);
            } finally {
                RedisLockUtil.unlock(lockName, lockId);
            }
        }

    }
}
